package com.shine.filter;

import com.alibaba.fastjson2.JSON;
import com.shine.status.ResponseStatus;
import com.shine.utils.Result;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @author huihui
 * @date 2025/10/14 16:11
 * @description BaseFilter
 */
public abstract class BaseFilter {

    public void handleException(HttpServletResponse response, ResponseStatus status) throws IOException {
        // ======== 跨域头 =========
        response.setHeader("Access-Control-Allow-Origin", "*"); // 或者 request.getHeader("Origin") 传进来更安全
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");

        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(Result.error(status.getCode(), status.getName())));
    }

}
